var http = require('http');
var querystring = require('querystring');
var util = require('util');

var postHTML =
  '<html><head><meta charset="utf-8"><title>Node.js 实例</title></head>' +
  '<body>' +
  '<form method="post">' +
  '网站名： <input name="name"><br>' +
  '网站 URL： <input name="url"><br>' +
  '<input type="submit">' +
  '</form>' +
  '</body></html>';

http.createServer((req,res) => {
	var post = '';
	req.on('data', chunk => { post += chunk; });
	req.on('end', () => {
		post = querystring.parse(post);
		if (post.name && post.url) {
			res.writeHead(200, {'content-type':'text/plain'});
			res.write('website:' + post.name);
			res.write("<br>");
			res.write('url:' + post.url);
		} else {
			res.write(postHTML);
		}
		res.end();
	});
}).listen(8080);

console.log('server started');


